package com.taxi.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.taxi.bean.VehiculoBean;
import com.taxi.service.PaqueteBusinessDelegate;
import com.taxi.service.VehiculoService;

public class VehiculoAction extends ActionSupport implements ServletRequestAware {
 
	private static final long serialVersionUID = 1L;
	
	private HttpServletRequest request; 
	private VehiculoService vehiculoservice = PaqueteBusinessDelegate.getVehiculoService();
	
	private String msg;
	private boolean success;
	
	private List<VehiculoBean> lstVehiculo ;
	
	
	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}
	
	public String registrarVehiculo() throws Exception {
		Map<String,Object> sesion = ActionContext.getContext().getSession();
		System.out.println("ENTRO AL REGISTRAR");
		
		VehiculoBean bean = vehiculoservice.codigoAutogenerado();
		VehiculoBean vehiculo = new VehiculoBean();
		
		if(bean.getCodigo() == null){
			bean.setCodigo("VEH0000001");
		}
		else{
			vehiculo.setCodigo(bean.getCodigo());
		}

	/*	System.out.println(this.request.getParameter("ruc"));
		System.out.println(this.request.getParameter("razonsocial"));
		System.out.println(this.request.getParameter("direccion")); 
		System.out.println(this.request.getParameter("banco"));
		System.out.println(this.request.getParameter("combustible"));
		
        vehiculo.setRuc(this.request.getParameter("ruc"));
        vehiculo.setRazonsocial(this.request.getParameter("razonsocial"));
        vehiculo.setDireccion(this.request.getParameter("direccion"));
        vehiculo.setNumero(this.request.getParameter("numero"));
        vehiculo.setUrbanizacion(this.request.getParameter("urbanizacion"));
        vehiculo.setTipocuenta(this.request.getParameter("tipocuenta"));
        vehiculo.setNumerocuenta(this.request.getParameter("numerocuenta"));
        vehiculo.setBanco(this.request.getParameter("banco"));*/
		
		
        vehiculo.setNumeroplaca(this.request.getParameter("numeroplaca"));//////////////
        vehiculo.setNumeromotor(this.request.getParameter("numeromotor"));//////////////
        vehiculo.setNumerochasis(this.request.getParameter("numerochasis"));///////////
        vehiculo.setNumeroasiento(Integer.parseInt(this.request.getParameter("numeroasiento")));//////////
        vehiculo.setNumeropuerta(Integer.parseInt(this.request.getParameter("numeropuerta")));////////////
        vehiculo.setCarga(Integer.parseInt(this.request.getParameter("carga")));///////////
        vehiculo.setAnio(this.request.getParameter("anio"));/////////////////
        vehiculo.setCombustible(this.request.getParameter("combustible"));//////////////
        vehiculo.setEmpresa(this.request.getParameter("empresa"));
        
        //crear empresa

		try {
			vehiculoservice.registrarVehiculo(vehiculo);
			
			setSuccess(true);
			setMsg("Vehiculo registrado con exito");
		} catch (Exception e) {
			e.printStackTrace();
			setSuccess(false);
			setMsg("Error al registrar vehiculo");
		}
		
		 return SUCCESS;
	}

	
	public String actualizarVehiculo() throws Exception {

		System.out.println("ENTRO AL Actualizar");
		 
		VehiculoBean vehiculo = new VehiculoBean();

		System.out.println(""+this.request.getParameter("codigo"));
		System.out.println(""+this.request.getParameter("numeroplaca"));
		System.out.println(""+this.request.getParameter("numeromotor"));
		System.out.println(""+this.request.getParameter("numerochasis"));
		System.out.println(""+this.request.getParameter("carga")); 
		
		vehiculo.setCodigo(this.request.getParameter("codigo"));
        vehiculo.setNumeroplaca(this.request.getParameter("numeroplaca"));//////////////
        vehiculo.setNumeromotor(this.request.getParameter("numeromotor"));//////////////
        vehiculo.setNumerochasis(this.request.getParameter("numerochasis"));///////////
        vehiculo.setNumeroasiento(Integer.parseInt(this.request.getParameter("numeroasiento")));//////////
        vehiculo.setNumeropuerta(Integer.parseInt(this.request.getParameter("numeropuerta")));////////////
        vehiculo.setCarga(Integer.parseInt(this.request.getParameter("carga")));///////////
        vehiculo.setAnio(this.request.getParameter("anio"));/////////////////
        vehiculo.setCombustible(this.request.getParameter("combustible"));//////////////
        vehiculo.setEmpresa(this.request.getParameter("empresa"));
        
        //actualizar  empresa

		try {
			vehiculoservice.actualizarVehiculo(vehiculo);
			
			setSuccess(true);
			setMsg("Vehiculo Actualizado con exito");
		} catch (Exception e) {
			e.printStackTrace();
			setSuccess(false);
			setMsg("Error al actualizar vehiculo");
		}
		
		 return SUCCESS;
	}
	
	public String eliminarVehiculo() throws Exception {

		System.out.println("ENTRO AL eliminar");
		
		VehiculoBean bean = vehiculoservice.codigoAutogenerado();
		VehiculoBean vehiculo = new VehiculoBean();

		vehiculo.setCodigo(this.request.getParameter("codigo")); ///Codigo se jala de una grilla
        
        //eliminar empresa

		try {
			vehiculoservice.eliminarVehiculo(vehiculo);
			
			setSuccess(true);
			setMsg("Vehiculo eliminada con exito");
		} catch (Exception e) {
			e.printStackTrace();
			setSuccess(false);
			setMsg("Error al eliminada Vehiculo");
		}
		 return SUCCESS;
	}
	

	public String listadoVehiculo() throws Exception {

		System.out.println("ENTRO AL Listado");
		 
		try {
			lstVehiculo = vehiculoservice.listadoVehiculo();
			setSuccess(true); 

		} catch (Exception e) {
			e.printStackTrace();
			setSuccess(false);
			setMsg("Error al cargar Vehiculos");
		} 
		 return SUCCESS;
	}

	
	public VehiculoService getVehiculoservice() {
		return vehiculoservice;
	}

	public void setVehiculoservice(VehiculoService vehiculoservice) {
		this.vehiculoservice = vehiculoservice;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public List<VehiculoBean> getLstVehiculo() {
		return lstVehiculo;
	}

	public void setLstVehiculo(List<VehiculoBean> lstVehiculo) {
		this.lstVehiculo = lstVehiculo;
	}
	
	
	
	
}
